package org.project.s.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.Doreport;
import org.project.pojo.Fit;
import org.project.pojo.Order;
import org.project.pojo.Ordertime2;
import org.project.pojo.Remaintimes;
import org.project.pojo.Showreport;
import org.project.service.user.Order1Service;
import org.project.service.user.OrdertimeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Controller
@RequestMapping("/Ordertimecontroller1")
public class Ordertimecontroller1 {
     @Resource
     private Order1Service order1Service;
     
     @Resource
     private OrdertimeService ordertimeService;
     
     
     
     /**
      * 	* @方法名:更改时间（恢复原预约时间）
      * 	* @参数:
      * 	* @输出:
      * 	* @备注:
      * 	* @作者: 钱程浩
      * 	* @时间: 2018年2月1 11:35:20
      * 	* @修改:
      * 	*/
     @RequestMapping("/editordertime")
     public String editordertime(@Param("orderid")String orderid){
    	 
    	//通过orderid得到 预约时间
 		Order Order=order1Service.selectorder1(orderid);
 		String orderTime=Order.getOrderTime();
 		String shopName=Order.getShopName();
 		//店铺id
 		int shopid=ordertimeService.selshopid(shopName);
 		
 		//得到预约项目的fitnum
 		int fitnum=ordertimeService.selorderfitnum(orderid);
 		
 		Ordertime2 Ordertime3=counttime(orderTime,fitnum);//计算时间段
 		
 		
 		//拿到 剩余时间表中的 当天，该店铺的剩余时间
 		List<String> list2=selectdeletedtimes(shopid,Ordertime3.getDate());
 		
 		//便利剩余时间表
 		Iterator<String> it2=list2.iterator();
 		while(it2.hasNext()){
 			System.out.println("ssssssssssssssssssssssssssssssssssssssssssssssss");
 			String time4=it2.next();
 			System.out.println(time4);
 			System.out.println(time4);
 			Date time5=null;
 			try {
 				 time5=new SimpleDateFormat("HH:mm").parse(time4);
 			} catch (ParseException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 			
 			if(null!=time5){
 				  if((time5.before(Ordertime3.getEndtime())&&time5.after(Ordertime3.getOrdertime()))||time5.equals(Ordertime3.getOrdertime())){
 					  System.out.println(time5+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
 					  //执行修改(剩余时间表)
 					  Remaintimes Remaintimes=new Remaintimes(0,shopid,Ordertime3.getDate(),time4);
 					  
 					  ordertimeService.cancelremaintime(Remaintimes);
 					    
 				  }
 				}
 			
 			
 		}
 		
 		//删除操作表记录
 		SimpleDateFormat sim2=new SimpleDateFormat("HH:mm");
 		String time6=sim2.format(Ordertime3.getOrdertime());
 		Doreport Doreport=new Doreport(0,shopid,Ordertime3.getDate(),time6,null);
 		ordertimeService.deletedoreport(Doreport);
 		
 		//恢复时间完成
 		
 		
 		//更改订单状态
 		order1Service.dosx(orderid);
 		
     
        return "redirect:/order1controller/selectyewzf";
     
     }
    	 
    	 
    	 
     @RequestMapping("dopbtimes")
     public String dopbtimes(@Param("date")String date,@Param("shopid")String shopid){
    	 HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    	 String arr[]=req.getParameterValues("time");
    	 System.out.println(arr);
    	 System.out.println(date+"date");
    	 System.out.println(shopid+"shopid");
    	 if(null!=arr){
    	 for(int i=0;i<arr.length;i++){
    		 String time=arr[i];
    		 Remaintimes Remaintimes=new Remaintimes(0,Integer.valueOf(shopid),date,time);
    		 int j=ordertimeService.pbtimes(Remaintimes);
    		 if(j>0){
    			 Doreport Doreport=new Doreport(0,Integer.valueOf(shopid),date,time,"屏蔽");
    			 ordertimeService.insertdoreport2(Doreport);
    			 
    		 }
    		 
    		 
    	 }
    	 }
    	 return "mainfra";
    	 
    	 
     }
    	 
    	 
    	 @RequestMapping("/showpbtimes")
    	 public String showpbtimes(HttpServletRequest request){
    		 
    		 List<Showreport>list =ordertimeService.showpbreport();
    		 
    		 request.setAttribute("pblist", list);
    		 
    		 return "showpbtimes";
    	 }
    	 
    	 
    	 
    	 
    	 @RequestMapping("/huifu")
    	 public String huifu(@Param("id")String id){
    		 
    		 Doreport doreport=new Doreport(Integer.valueOf(id),0,null,null,null);
    		 Doreport doreport2=ordertimeService.selectdoreportbyid(doreport);
    		 int shopid=doreport2.getShopid();
    		 String dodate=doreport2.getDodate();
    		 String dotime=doreport2.getDotime();
    		 
    		 Remaintimes Remaintimes=new Remaintimes(0,shopid,dodate,dotime);
    		 ordertimeService.huifupb(Remaintimes);
    		 ordertimeService.deldoreport(doreport);
    		 
    		 return"redirect:/Ordertimecontroller1/showpbtimes";
    		 
    	 }
    	 
     
	  
	  
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
     //通过shopid，详细日期 查询剩余的时间
     public List<String> selectdeletedtimes(int shopid,String date){
    	 Remaintimes remaintimes1=new Remaintimes(0,shopid,date,null);
    	 List<String> deletedlist=ordertimeService.selectyuyuedtimes(remaintimes1);
    	 
    	 return deletedlist;
     }
     
		
	  
	  public  Ordertime2   counttime(String orderTime,int fitnum){
		String arr[]=  orderTime.split(" ");
		  String  date=arr[0]; 
		  String  ordertime0=arr[1]; 
		  
		  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		  Date ordertime=null;
		  try {
			ordertime=sdf.parse(ordertime0);       //预约时间（Date）
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
		    Date endtime= countendtime(ordertime0,fitnum);  //结束时间（Date）
		  
		    Ordertime2 Ordertime2=new Ordertime2(date,ordertime,endtime);
		   
		 return Ordertime2;
		 
		  
	  }
	
	  
     
	  /**
		 * 	* @方法名:计算该类型项目的结束时间
		 * 	* @参数:开始时间、项目类型
		 * 	* @输出:
		 * 	* @备注:
		 * 	* @作者: 钱程浩
		 * 	* @时间: 2018年1月29 6:42:23
		 * 	* @修改:
		 * 	*/
		public  Date countendtime(String starttime,int fitnum) {
			Fit fit0=new Fit(0,fitnum,0);
			Fit fit=ordertimeService.requiredtime(fit0);
			int requiredtime=fit.getRequiredtime();
			
			//计算结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			 Date time=null;
			try {
				time = sdf.parse(starttime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 Calendar cal = Calendar.getInstance();
			 cal.setTime(time);
			 cal.add(Calendar.MINUTE, +requiredtime);
			  time = cal.getTime();
			  
			 return time;
			 
		}

	  
     
 
     
}

